package com.twu.biblioteca.commands;

import com.google.common.base.Optional;
import com.twu.biblioteca.Biblioteca;
import com.twu.biblioteca.LibraryNumber;
import com.twu.biblioteca.Password;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class LoginCommand extends BibliotecaCommand {
    public static final String SUCCESS_MESSAGE = "You have successfully logged in.";
    public static final String FAILURE_MESSAGE = "Failed to login.";

    public LoginCommand(InputStream input, PrintStream output, Biblioteca biblioteca) {
        super("Log into your account", input, output, biblioteca);
    }

    public boolean run() {
        Scanner scanner = new Scanner(input);

        output.print("What is your Library Number?: ");
        String number = scanner.nextLine();

        output.print("What is your Password?: ");
        String password = scanner.nextLine();

        try {
            LibraryNumber libraryNumber = LibraryNumber.parseNumberFromString(number);

            output.println(biblioteca.login(libraryNumber, new Password(password))
                    ? SUCCESS_MESSAGE
                    : FAILURE_MESSAGE );

        } catch (IllegalArgumentException ex) {
            output.println(FAILURE_MESSAGE);
        }

        return false;
    }
}
